import wx
import wx.media
from Constant import *
from PublicMethod import *
'''
Frame_Rank : User can show rank of player. Through clicking left and right key on keyboard, 

             user can view before or next page.
'''
class Frame_Rank(wx.Panel):
    '''
    Constructor : Frame_Rank
        parent : the main frame of this
    '''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1, size = (parent.GetSize()[0], parent.GetSize()[1] - 20))
        self._001_initialInstanceVariable(parent)
        self._002_bindAction()
        self._003_createBackground()
        self._004_loadFromFile()
        self._005_createImgRank()
        self._006_createRankObj()
        self._007_createModal()
        self._008_createNextBeforeObj()
        self._009_createListTitleImg()
        self.ACT001_setListPageShow(0)
        self.ACT004_setIsShowObj()
    '''
    function _001 : initialize the variables used for this object
        main : the main frame
    '''
    def _001_initialInstanceVariable(self, main):
        #pointer of main frame
        self.main = main
        #directory of storing image of rank
        self.dirImgRank = "img rank"
        #list of name + score
        self.listNameScore = []
        #list of name + score object
        self.listNameScoreObj = []
        #list of medal
        self.listMedalObj = []
        #list of image title
        self.listTitleObj = []
        #image of before
        self.imgBefore = None
        #image of next
        self.imgNext = None
        #constant of showing number of list a time
        self.NUM_SHOW_LIST = 7
        #record current first rank of showing
        self.curFirstRankShow = 0
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        self.main.Bind(wx.EVT_KEY_DOWN, self.SLOT001_OnKeyClick)
    '''
    function _003 : create background image of this frame
    '''
    def _003_createBackground(self):
        image = PublicMethod.ACT001_getBmpImage(FILE_NAME_BACKGROUND, self.GetSize()[0], self.GetSize()[1])
        self.imgBackground = wx.StaticBitmap(parent=self, bitmap=image)
        self.imgBackground.Show(False)
        self.imgBackground.Show(True)
    '''
    function _004 : load all player-score information from file
    '''
    def _004_loadFromFile(self):
        if os.path.isfile(FILE_SCORE) :  #file exists, loading file
            isEnd = False
            fp = file(FILE_SCORE)
            while not isEnd :
                getLine = fp.readline()
                if len(getLine) != 0 :  #store each name and score to list
                    list = getLine.split(':')
                    self.listNameScore.append(list)
                else :
                    isEnd = True
            fp.close()
    '''
    function _005 : create title image of rank
    '''
    def _005_createImgRank(self):
        fileName = self.dirImgRank + "/img_rank_board.png"
        image = PublicMethod.ACT001_getBmpImage(fileName)
        pos = ((self.GetSize()[0] - image.GetSize()[0]) / 2, 10)
        self.titleImage = wx.BitmapButton(self, -1, image, pos = pos, style = wx.FRAME_SHAPED)
        self.titleImage.Enable(False)
    '''
    function _006 : create each object of showing rank
    '''
    def _006_createRankObj(self):
        #create list of label
        startX = 130
        pos = [startX, self.titleImage.GetPosition()[1] + self.titleImage.GetSize()[1] + 100]
        size = [450, 40]
        textFont = wx.Font(18, wx.NORMAL, wx.NORMAL, wx.BOLD)
        labelAttrSize = [(100, 40), (220, 40), (210, 40)]
        indexRow = 0
        while indexRow < self.NUM_SHOW_LIST :
            indexLabel = 0
            pos[0] = startX
            list = []
            while indexLabel < labelAttrSize.__len__() :
                 label = wx.StaticText(self.imgBackground, -1, size = labelAttrSize[indexLabel], pos = pos, 
                                       style = wx.ALIGN_LEFT | wx.ST_NO_AUTORESIZE | wx.BORDER_RAISED)
                 label.SetBackgroundColour("black")
                 label.SetFont(textFont)
                 label.SetForegroundColour("cyan")
                 list.append(label)
                 pos[0] = pos[0] + labelAttrSize[indexLabel][0] + 10
                 indexLabel = indexLabel + 1
            self.listNameScoreObj.append(list)
            pos[1] = pos[1] + size[1] + 15
            indexRow = indexRow + 1
    '''
    function _007 : create modal - gold, silver and copper modal
    '''
    def _007_createModal(self):
        list = ["gold.png", "silver.png", "copper.png"]
        index = 0
        while index < list.__len__() : 
            fileName = self.dirImgRank + "/" + list[index]
            image = PublicMethod.ACT001_getBmpImage(fileName, 55, 50)
            pos = (self.listNameScoreObj[index][0].GetPosition()[0] - 65, self.listNameScoreObj[index][0].GetPosition()[1] - 10)
            label = wx.BitmapButton(self, -1, image, pos = pos, style = wx.FRAME_SHAPED)
            label.Enable(False)
            self.listMedalObj.append(label)
            index = index + 1
    '''
    function _008 : create image of next and before
    '''
    def _008_createNextBeforeObj(self):
        posY = self.GetSize()[1] - 100
        #create before image
        image = PublicMethod.ACT001_getBmpImage(self.dirImgRank + "/before.png")
        self.imgBefore = wx.BitmapButton(self, -1, image, pos = (0, posY), style = wx.FRAME_SHAPED)
        self.imgBefore.Enable(False)
        #create next image
        image = PublicMethod.ACT001_getBmpImage(self.dirImgRank + "/next.png")
        self.imgNext = wx.BitmapButton(self, -1, image, pos = (self.GetSize()[0] - 120, posY), style = wx.FRAME_SHAPED)
        self.imgNext.Enable(False)
    '''
    function _009 : create list title of rank showing
    '''
    def _009_createListTitleImg(self):
        list = ["img_title_rank.png", "img_title_playerName.png", "img_title_score.png"]
        index = 0
        while index < list.__len__() :
            fileName = self.dirImgRank + "/" + list[index]
            image = PublicMethod.ACT001_getBmpImage(fileName)
            if self.listNameScoreObj[0][index].GetSize()[0] < image.GetSize()[0] :
                posX = self.listNameScoreObj[0][index].GetPosition()[0] - 10
            else :
                posX = self.listNameScoreObj[0][index].GetPosition()[0] - 5 + \
                       (self.listNameScoreObj[0][index].GetSize()[0] - image.GetSize()[0]) / 2
            pos = (posX, self.listNameScoreObj[0][index].GetPosition()[1] - 60)
            btn = wx.BitmapButton(self, -1, image, pos = pos, style = wx.FRAME_SHAPED)
            btn.Enable(False)
            self.listTitleObj.append(btn)
            index = index + 1
    '''
    function _010 : destroy all object of this class
    '''
    def _010_destroy(self):
        #clear list of name and score object
        while self.listNameScoreObj.__len__() > 0 :
            while self.listNameScoreObj[0].__len__() > 0 :
                self.listNameScoreObj[0][0].Destroy()
                self.listNameScoreObj[0].__delitem__(0)
            self.listNameScoreObj.__delitem__(0)
        #clear title image
        self.titleImage.Destroy()
        #clear before and next image
        self.imgBefore.Destroy()
        self.imgNext.Destroy()
        #clear modal image
        while self.listMedalObj.__len__() > 0 :
            self.listMedalObj[0].Destroy()
            self.listMedalObj.__delitem__(0)
        #clear list title
        while self.listTitleObj.__len__() > 0 :
            self.listTitleObj[0].Destroy()
            self.listTitleObj.__delitem__(0)
        #clear background image
        self.imgBackground.Destroy()
        self.Destroy()
    '''
    function ACT001 : set current showing of Rank list
    '''
    def ACT001_setListPageShow(self, startIndex):
        #clear current showing
        indexRow = 0
        while indexRow < self.listNameScoreObj.__len__() :
            indexCol = 0
            while indexCol < self.listNameScoreObj[indexRow].__len__() :
                self.listNameScoreObj[indexRow][indexCol].SetLabel("")
                indexCol = indexCol + 1
            indexRow = indexRow + 1
        #set rank showing
        index = 0
        while index < self.NUM_SHOW_LIST :
            scoreIndex = startIndex + index
            if scoreIndex < self.listNameScore.__len__() :
                #set rank
                self.listNameScoreObj[index][0].SetLabel(str(scoreIndex + 1))
                #set player name
                self.listNameScoreObj[index][1].SetLabel(self.listNameScore[scoreIndex][0])
                #set score
                self.listNameScoreObj[index][2].SetLabel(self.listNameScore[scoreIndex][1])
            index = index + 1
    '''
    function ACT002 : show next page of rank list
    '''
    def ACT002_showNext(self):
        if self.curFirstRankShow + self.NUM_SHOW_LIST < self.listNameScore.__len__() :
            self.curFirstRankShow = self.curFirstRankShow + self.NUM_SHOW_LIST
            self.ACT001_setListPageShow(self.curFirstRankShow)
    '''
    function ACT003 : show before page of rank list
    '''
    def ACT003_showBefore(self):
        if self.curFirstRankShow - self.NUM_SHOW_LIST >= 0 :
            self.curFirstRankShow = self.curFirstRankShow - self.NUM_SHOW_LIST
            self.ACT001_setListPageShow(self.curFirstRankShow)
    '''
    function ACT004 : set image object - modal, next and before image to show/disappear
    '''
    def ACT004_setIsShowObj(self):
        #set show of image before
        if self.curFirstRankShow == 0 :
            self.imgBefore.Show(False)
        else :
            self.imgBefore.Show(True)
        #set show of image next
        if self.curFirstRankShow + self.NUM_SHOW_LIST < self.listNameScore.__len__() :
            self.imgNext.Show(True)
        else :
            self.imgNext.Show(False)
        #set show of modal
        isShow = False
        if self.curFirstRankShow == 0 :
            isShow = True
        index = 0
        while index < self.listMedalObj.__len__() :
            self.listMedalObj[index].Show(isShow)
            index = index + 1
    '''
    function SLOT001 : slot of clicking key on keyboard
    '''
    def SLOT001_OnKeyClick(self, event):
        key = event.GetKeyCode()
        if key == wx.WXK_LEFT :  #click key - left
            self.ACT003_showBefore()
            self.ACT004_setIsShowObj()
            self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == wx.WXK_RIGHT :  #click key - right
            self.ACT002_showNext()
            self.ACT004_setIsShowObj()
            self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == wx.WXK_ESCAPE :  #click key - Esc
            main = self.main
            self._010_destroy()
            from Frame_Option import Frame_Option
            Frame_Option(main)
            
'''
main program
'''
if __name__ == "__main__" :
    app = wx.PySimpleApp()
    from Main import Main
    frame = Main()
    Frame_Rank(frame)
    frame.Show()
    app.MainLoop()